home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / fragrouter / libpcap-0.4 / pcap-nit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-26  |  5.6 KB  |  245 lines

  1. /*
  2.  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that: (1) source code distributions
  7.  * retain the above copyright notice and this paragraph in its entirety, (2)
  8.  * distributions including binary code include the above copyright notice and
  9.  * this paragraph in its entirety in the documentation or other materials
  10.  * provided with the distribution, and (3) all advertising materials mentioning
  11.  * features or use of this software display the following acknowledgement:
  12.  * ``This product includes software developed by the University of California,
  13.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14.  * the University nor the names of its contributors may be used to endorse
  15.  * or promote products derived from this software without specific prior
  16.  * written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  */
  21. #ifndef lint
  22. static const char rcsid[] =
  23.     "@(#) $Header: /cvs/fragrouter/libpcap-0.4/pcap-nit.c,v 1.1.1.1 1999/05/03 04:06:44 dugsong Exp $ (LBL)";
  24. #endif
  25.  
  26. #include <sys/types.h>
  27. #include <sys/time.h>
  28. #include <sys/timeb.h>
  29. #include <sys/file.h>
  30. #include <sys/ioctl.h>
  31. #include <sys/socket.h>
  32.  
  33. #include <net/if.h>
  34. #include <net/nit.h>
  35.  
  36. #include <netinet/in.h>
  37. #include <netinet/in_systm.h>
  38. #include <netinet/ip.h>
  39. #include <netinet/if_ether.h>
  40. #include <netinet/ip_var.h>
  41. #include <netinet/udp.h>
  42. #include <netinet/udp_var.h>
  43. #include <netinet/tcp.h>
  44. #include <netinet/tcpip.h>
  45.  
  46. #include <ctype.h>
  47. #include <errno.h>
  48. #include <stdio.h>
  49.  
  50. #include "pcap-int.h"
  51.  
  52. #include "gnuc.h"
  53. #ifdef HAVE_OS_PROTO_H
  54. #include "os-proto.h"
  55. #endif
  56.  
  57. /*
  58.  * The chunk size for NIT.  This is the amount of buffering
  59.  * done for read calls.
  60.  */
  61. #define CHUNKSIZE (2*1024)
  62.  
  63. /*
  64.  * The total buffer space used by NIT.
  65.  */
  66. #define BUFSPACE (4*CHUNKSIZE)
  67.  
  68. /* Forwards */
  69. static int nit_setflags(int, int, int, char *);
  70.  
  71. int
  72. pcap_stats(pcap_t *p, struct pcap_stat *ps)
  73. {
  74.  
  75.     *ps = p->md.stat;
  76.     return (0);
  77. }
  78.  
  79. int
  80. pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
  81. {
  82.     register int cc, n;
  83.     register struct bpf_insn *fcode = p->fcode.bf_insns;
  84.     register u_char *bp, *cp, *ep;
  85.     register struct nit_hdr *nh;
  86.     register int caplen;
  87.  
  88.     cc = p->cc;
  89.     if (cc == 0) {
  90.         cc = read(p->fd, (char *)p->buffer, p->bufsize);
  91.         if (cc < 0) {
  92.             if (errno == EWOULDBLOCK)
  93.                 return (0);
  94.             sprintf(p->errbuf, "pcap_read: %s",
  95.                 pcap_strerror(errno));
  96.             return (-1);
  97.         }
  98.         bp = p->buffer;
  99.     } else
  100.         bp = p->bp;
  101.  
  102.     /*
  103.      * Loop through each packet.  The increment expression
  104.      * rounds up to the next int boundary past the end of
  105.      * the previous packet.
  106.      */
  107.     n = 0;
  108.     ep = bp + cc;
  109.     while (bp < ep) {
  110.         nh = (struct nit_hdr *)bp;
  111.         cp = bp + sizeof(*nh);
  112.  
  113.         switch (nh->nh_state) {
  114.  
  115.         case NIT_CATCH:
  116.             break;
  117.  
  118.         case NIT_NOMBUF:
  119.         case NIT_NOCLUSTER:
  120.         case NIT_NOSPACE:
  121.             p->md.stat.ps_drop = nh->nh_dropped;
  122.             continue;
  123.  
  124.         case NIT_SEQNO:
  125.             continue;
  126.  
  127.         default:
  128.             sprintf(p->errbuf, "bad nit state %d", nh->nh_state);
  129.             return (-1);
  130.         }
  131.         ++p->md.stat.ps_recv;
  132.         bp += ((sizeof(struct nit_hdr) + nh->nh_datalen +
  133.             sizeof(int) - 1) & ~(sizeof(int) - 1));
  134.  
  135.         caplen = nh->nh_wirelen;
  136.         if (caplen > p->snapshot)
  137.             caplen = p->snapshot;
  138.         if (bpf_filter(fcode, cp, nh->nh_wirelen, caplen)) {
  139.             struct pcap_pkthdr h;
  140.             h.ts = nh->nh_timestamp;
  141.             h.len = nh->nh_wirelen;
  142.             h.caplen = caplen;
  143.             (*callback)(user, &h, cp);
  144.             if (++n >= cnt && cnt >= 0) {
  145.                 p->cc = ep - bp;
  146.                 p->bp = bp;
  147.                 return (n);
  148.             }
  149.         }
  150.     }
  151.     p->cc = 0;
  152.     return (n);
  153. }
  154.  
  155. static int
  156. nit_setflags(int fd, int promisc, int to_ms, char *ebuf)
  157. {
  158.     struct nit_ioc nioc;
  159.  
  160.     bzero((char *)&nioc, sizeof(nioc));
  161.     nioc.nioc_bufspace = BUFSPACE;
  162.     nioc.nioc_chunksize = CHUNKSIZE;
  163.     nioc.nioc_typetomatch = NT_ALLTYPES;
  164.     nioc.nioc_snaplen = p->snapshot;
  165.     nioc.nioc_bufalign = sizeof(int);
  166.     nioc.nioc_bufoffset = 0;
  167.  
  168.     if (to_ms != 0) {
  169.         nioc.nioc_flags |= NF_TIMEOUT;
  170.         nioc.nioc_timeout.tv_sec = to_ms / 1000;
  171.         nioc.nioc_timeout.tv_usec = (to_ms * 1000) % 1000000;
  172.     }
  173.     if (promisc)
  174.         nioc.nioc_flags |= NF_PROMISC;
  175.  
  176.     if (ioctl(fd, SIOCSNIT, &nioc) < 0) {
  177.         sprintf(ebuf, "SIOCSNIT: %s", pcap_strerror(errno));
  178.         return (-1);
  179.     }
  180.     return (0);
  181. }
  182.  
  183. pcap_t *
  184. pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf)
  185. {
  186.     int fd;
  187.     struct sockaddr_nit snit;
  188.     register pcap_t *p;
  189.  
  190.     p = (pcap_t *)malloc(sizeof(*p));
  191.     if (p == NULL) {
  192.         strcpy(ebuf, pcap_strerror(errno));
  193.         return (NULL);
  194.     }
  195.  
  196.     if (snaplen < 96)
  197.         /*
  198.          * NIT requires a snapshot length of at least 96.
  199.          */
  200.         snaplen = 96;
  201.  
  202.     bzero(p, sizeof(*p));
  203.     p->fd = fd = socket(AF_NIT, SOCK_RAW, NITPROTO_RAW);
  204.     if (fd < 0) {
  205.         sprintf(ebuf, "socket: %s", pcap_strerror(errno));
  206.         goto bad;
  207.     }
  208.     snit.snit_family = AF_NIT;
  209.     (void)strncpy(snit.snit_ifname, device, NITIFSIZ);
  210.  
  211.     if (bind(fd, (struct sockaddr *)&snit, sizeof(snit))) {
  212.         sprintf(ebuf, "bind: %s: %s", snit.snit_ifname,
  213.             pcap_strerror(errno));
  214.         goto bad;
  215.     }
  216.     p->snapshot = snaplen;
  217.     nit_setflags(p->fd, promisc, to_ms, ebuf);
  218.  
  219.     /*
  220.      * NIT supports only ethernets.
  221.      */
  222.     p->linktype = DLT_EN10MB;
  223.  
  224.     p->bufsize = BUFSPACE;
  225.     p->buffer = (u_char *)malloc(p->bufsize);
  226.     if (p->buffer == NULL) {
  227.         strcpy(ebuf, pcap_strerror(errno));
  228.         goto bad;
  229.     }
  230.     return (p);
  231.  bad:
  232.     if (fd >= 0)
  233.         close(fd);
  234.     free(p);
  235.     return (NULL);
  236. }
  237.  
  238. int
  239. pcap_setfilter(pcap_t *p, struct bpf_program *fp)
  240. {
  241.  
  242.     p->fcode = *fp;
  243.     return (0);
  244. }
  245.